package com.mingweicai.intergration.thread.orderprint;

import java.util.concurrent.Semaphore;

public class Print1 implements Runnable{

  Semaphore semaphoreF ;
  Semaphore semaphoreN ;
  int start ;
  int n;

  public Print1(Semaphore semaphoreF, Semaphore semaphoreN, int start, int n) {
    this.semaphoreF = semaphoreF;
    this.semaphoreN = semaphoreN;
    this.start = start;
    this.n = n;
  }

  @Override
  public void run() {
    try {
      for(int i=start; i<=n; i+=3){
        semaphoreF.acquire();
        System.out.println(i);
        semaphoreN.release();
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  public static void main(String args[]){
    Semaphore semaphoreA = new Semaphore(1);
    Semaphore semaphoreB = new Semaphore(0);
    Semaphore semaphoreC = new Semaphore(0);
    new Thread(new Print1(semaphoreA,semaphoreB,1,99)).start();
    new Thread(new Print1(semaphoreB,semaphoreC,2,99)).start();
    new Thread(new Print1(semaphoreC,semaphoreA,3,99)).start();
  }
}
